package Function.ItemUse;

import FCRendering.FCRBattleOrderConstant;
import Frame.battleanimation.AnimationUtil;
import Function.FCMutilPlayCache;
import Function.FCMutilPlayTankCache;
import VWorldGameStudio.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import static FCRendering.FCRMaster.*;

//继承AItemUse的类命名规则，类型名+名称  比如 FoodShenWan
public abstract class AItemUse{

    protected FCPlayerInfo keepPlayer;//持有人

    protected boolean isInTankBag = false;//是否物品放在坦克背包里

    protected int positionInBag = 0; //在背包的位置

    protected FCTank keepItemTank;//如果物品在坦克背包，那坦克是那个

    protected FCItem useItem;//正在使用的物品

    protected String itemName;

    public AItemUse setUpIt(FCPlayerInfo p, FCTank t, String itemName, int position, boolean isIntank){
        keepPlayer = p;
        keepItemTank = t;
        this.itemName = itemName;
        useItem = itemLib.getItemOfID(itemName);;
        positionInBag = position;
        this.isInTankBag = isIntank;
        return this;
    }

    public abstract ItemUseResult directUse();

    public abstract ItemUseResult useInMapTarget(int x, int y);

    public abstract ItemUseResult useForMonsterTarget(FCBattleOrder battleOrder, FCMonster m);

    public abstract ItemUseResult useForPlayerTarget(FCPlayerInfo p);

    public abstract ItemUseResult useForTankTarget(FCTank t);

    //获取全部被命中的怪物的列表
    public ArrayList<FCMonster> getHitFcMonsters(FCBattleOrder battleOrder, FCMonster m){
        FCPlayerBattleWrap wrap = FCPlayerBattleWrap.findBattleWrapByName(keepPlayer.getAliasUID(), keepPlayer.getName());
        int hitRate = keepPlayer.getHitRate(wrap);
        if(wrap.isBattleDriverTank()){
            //那就只计算坦克的命中值
            FCTank tank = tankLib.getTankOfID(keepPlayer.getPlayertank1(), keepPlayer.getPlayertankHostId());
            if(tank != null) {
                hitRate = tank.getHitRate(wrap, keepPlayer.getSpeed2(), wrap.getBattlePlayerSelectMenu());
                //TODO 还要加上职业的战车驾驶修正？
            }else {
                hitRate = 0;
            }
        }
        ArrayList<FCMonster> now2AliveMonsters = getSortNowAliveMonsters(hitRate, battleOrder, m);
        ArrayList<FCMonster> toAttckMonster = WeaponTypeItemUse.getTargetMonster(useItem.getAttackNum(), now2AliveMonsters);
        ArrayList<FCMonster> ret = new ArrayList<>(0);
        for (FCMonster ms : toAttckMonster) {
            if (battleOrder.randomV2 * 100 <= ms.getAttackRate(hitRate)) {
                ret.add(ms);
            }else {
                if(toAttckMonster.indexOf(ms) == 0){
                    break;
                }
            }
        }
        return ret;
    }

    public ArrayList<FCMonster> getSortNowAliveMonsters(int hitRate, FCBattleOrder battleOrder, FCMonster m){
        ArrayList<FCMonster> nowAliveMonsters = new ArrayList<>(0);
        for (int i = 0; i < fcv.getBattleMonster().size(); i++) {
            FCMonster f = fcv.getBattleMonster().get(i);
            if (!f.getIsDeath()) {
                nowAliveMonsters.add(f);
            }
        }
        int mIndex = fcv.getBattleMonster().indexOf(m);
        Integer[] mp = fcv.getBattleMonsterCoor().get(mIndex);
        int scx = (int) (mp[0] + mp[2] / 2);
        int scy = (int) (mp[1] + mp[3] / 2);
        //SETP 0 按距离排序
        ArrayList<Integer[]> distances = new ArrayList<>();
        for (int i = 0; i < nowAliveMonsters.size(); i++) {
            Integer[] in = new Integer[2];
            in[0] = i;
            mIndex = fcv.getBattleMonster().indexOf(nowAliveMonsters.get(i));
            mp = fcv.getBattleMonsterCoor().get(mIndex);
            int cx = (int) (mp[0] + mp[2] / 2);
            int cy = (int) (mp[1] + mp[3] / 2);
            in[1] = (int) AnimationUtil.getDistance(new int[]{scx, scy}, new int[]{cx, cy});
            distances.add(in);
        }
        Comparator com2 = new Comparator<Integer[]>() {
            @Override
            public int compare(Integer[] o1, Integer[] o2) {
                return o1[1] - o2[1];
            }
        };
        Collections.sort(distances, com2);
        ArrayList<FCMonster> now2AliveMonsters = new ArrayList<>(nowAliveMonsters.size());
        for (Integer[] integers : distances) {
            now2AliveMonsters.add(nowAliveMonsters.get(integers[0]));
        }
        return now2AliveMonsters;
    }


    //用完后，需要从背包删除的
    protected void useAfter(){
        if(useItem.getCanUseTimes() == 1) {
            if (isInTankBag) {
                keepItemTank.insertItemToKnapsack(positionInBag, "null");
                keepItemTank.reInitShuxing();
                gameSave.delTankItemClone(itemName);
            } else {
                keepPlayer.insertItemToKnapsack(positionInBag, "null");
            }

        }
    }
}
